package com.guguskins.back.controller;
import com.guguskins.back.common.annotation.Authorized;
import com.guguskins.back.entity.GuDisCountOrUser;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.service.GuDisCountOrUserService;
import com.guguskins.back.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 用户表和折扣券关联表 前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-07-14
 */
@RestController
@RequestMapping("/guDisCountOrUser")
public class GuDisCountOrUserController {
    @Autowired
    private GuDisCountOrUserService disCountOrUserService;

    @PostMapping("/insert")
    @Authorized
    public ResultVo insert(@RequestBody GuDisCountOrUser disCountOrUser) {
        disCountOrUserService.insert(disCountOrUser);
        return ResultVo.success("添加成功");
    }

    @GetMapping("/back/list")
    public ResultVo backList(
            @RequestParam(value = "pageNum",required = false)int pageNum,
            @RequestParam(value = "pageSize",required = false)int pageSize,
            @RequestParam(value = "userName",required = false)String userName,
            @RequestParam(value = "disCountName",required = false)String disCountName,
            @RequestParam(value = "disCountState",required = false)String disCountState
    ) {

        return ResultVo.success(disCountOrUserService.findBypage(pageNum,pageSize,userName,disCountName,disCountState), "查询成功");
    }

    @PostMapping("/back/update")
    public ResultVo backUpdate(@RequestBody GuDisCountOrUser disCountOrUser) {
//        System.out.println(disCountOrUser.getDuId());
        disCountOrUserService.updateById(disCountOrUser);
        return ResultVo.success("修改成功");
    }

    @GetMapping("/back/delect")
    public ResultVo backDelect(@RequestParam(value = "id", required = false) Integer id) {
        disCountOrUserService.removeById(id);
        return ResultVo.success("删除成功");
    }

    @GetMapping("/findEffective")
    public ResultVo findEffective(HttpServletRequest request) {
        String token = request.getHeader("token");
        Integer userId = JwtUtil.verifyToken(token).get("userId", Integer.class);
        List<GuDisCountOrUser> disCountOrUser = disCountOrUserService.findByDIdOrUserId(userId, 1);
        if (disCountOrUser.size() > 0) {
            return ResultVo.success(disCountOrUser, "查找成功");
        } else {
            return ResultVo.success(disCountOrUser, "没有优惠券可用");
        }
    }

    /**
     * 优惠券是否是用户的是否可用
     *
     * @param counponCode
     * @return
     */
    @GetMapping("/discount/available")
    public ResultVo findByDisCount(
            @RequestParam(value = "counponCode") String counponCode,
            @RequestParam(value = "threshold")BigDecimal threshold,
            HttpServletRequest request) {
        String token = request.getHeader("token");
        Integer userId = JwtUtil.verifyToken(token).get("userId", Integer.class);
        int available = disCountOrUserService.findByDisCount(userId, counponCode,threshold);
        if (available == 1) {
            return ResultVo.fail("优惠券已过期");
        } else if (available == 0) {
            return ResultVo.fail("这个用户没有这个优惠券");
        }
        else if(available==3){
            return ResultVo.fail("使用门槛不够");
        }else {
            return ResultVo.success("该优惠券可用");
        }

    }

}
